import 'package:flutter/material.dart';
import 'constant.dart' show Constants;
import 'constant.dart' show Appcolors;
import 'chatlist_page.dart';
import 'package:flutter_app/ContactPage.dart';

enum ActionItems { GROUP_CHAT, ADD_FRIENDS, QR_SCAN, PAYMENT, HELP }

class ThirdPage extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
//    return new HomeScreen();
    return MaterialApp(
      title: "微信",
      theme: ThemeData.light().copyWith(
          primaryColor: Color(0xff303030), canvasColor: Color(0xff303030)),
      home: HomeScreen(),
    );
  }
}

class HomeScreen extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return new _HomeScreenState();
  }
}

class _HomeScreenState extends State<HomeScreen> {
  List<NavigationIconView> _navigationViews;
  int _currentIndex = 0;

  //ViewPager
  PageController _pageController;
  List<Widget> _pageViews;

  @override
  void initState() {
    super.initState();
    _navigationViews = [
      NavigationIconView(
        title: "微信",
        icon: Icon(IconData(0xe615, fontFamily: Constants.IconFontFamily)),
        activeIcon:
            Icon(IconData(0xe62f, fontFamily: Constants.IconFontFamily)),
      ),
      NavigationIconView(
        title: "通讯录",
        icon: Icon(IconData(0xe655, fontFamily: Constants.IconFontFamily)),
        activeIcon:
            Icon(IconData(0xe6c2, fontFamily: Constants.IconFontFamily)),
      ),
      NavigationIconView(
        title: "发现",
        icon: Icon(IconData(0xe746, fontFamily: Constants.IconFontFamily)),
        activeIcon:
            Icon(IconData(0xe603, fontFamily: Constants.IconFontFamily)),
      ),
      NavigationIconView(
        title: "我的",
        icon: Icon(IconData(0xe7ab, fontFamily: Constants.IconFontFamily)),
        activeIcon:
            Icon(IconData(0xe61e, fontFamily: Constants.IconFontFamily)),
      ),
    ];
    _pageController = PageController(initialPage: _currentIndex);
    _pageViews = [
      ChatList(),
      ContactState(),
      Container(
        color: Colors.yellow,
      ),
      Container(
        color: Colors.blue,
      )
    ];
  }

  @override
  Widget build(BuildContext context) {
    const selectColor = Color(Appcolors.TabIconActive);

    final BottomNavigationBar bottomNavigationBar = BottomNavigationBar(
      items:
          _navigationViews.map((NavigationIconView view) => view.item).toList(),
      currentIndex: _currentIndex,
      fixedColor: selectColor,
      type: BottomNavigationBarType.fixed,
      onTap: (int index) {
        setState(() {
          _currentIndex = index;
          _pageController.animateToPage(_currentIndex,
              duration: Duration(milliseconds: 300), curve: Curves.easeInOut);
        });
      },
    );

    _buildPopMenueItem(int iconfont, String title) {
      return Row(
        children: <Widget>[
          Icon(IconData(iconfont)),
          Container(
            width: 12,
          ),
          Text(title),
        ],
      );
    }

    _buildPopItemView(int iconfont, String title, ActionItems value) {
      return PopupMenuItem(
        child: _buildPopMenueItem(iconfont, title),
        value: value,
      );
    }

    return new MaterialApp(
      title: "ThirdPage ",
      theme: new ThemeData(primaryColor: Colors.blue),
      home: new Scaffold(
        appBar: AppBar(
          title: Text("微信"),
          elevation: 0,
          backgroundColor: Color(0xff303030),
          actions: <Widget>[
            IconButton(
              icon: Icon(Icons.search),
              onPressed: () {
                print("click search icon");
              },
            ),
            PopupMenuButton(
              itemBuilder: (BuildContext context) {
                return <PopupMenuItem<ActionItems>>[
                  _buildPopItemView(0xe69e, "发起群聊", ActionItems.GROUP_CHAT),
                  _buildPopItemView(0xe69e, "添加朋友", ActionItems.ADD_FRIENDS),
                  _buildPopItemView(0xe69e, "扫一扫", ActionItems.QR_SCAN),
                  _buildPopItemView(0xe69e, "收付款", ActionItems.PAYMENT),
                  _buildPopItemView(0xe69e, "帮助与反馈", ActionItems.HELP),
                ];
              },
              icon: Icon(Icons.add),
              onSelected: (ActionItems actionItem) {
                print(actionItem);
              },
            ),
          ],
        ),
        body: PageView.builder(
          itemBuilder: (BuildContext context, int index) {
            return _pageViews[index];
          },
          controller: _pageController,
          itemCount: _pageViews.length,
          onPageChanged: (int index) {
            setState(() {
              _currentIndex = index;
            });
          },
        ),
        bottomNavigationBar: bottomNavigationBar,
      ),
    );
  }
}

class NavigationIconView {
  final BottomNavigationBarItem item;

  NavigationIconView({Key key, String title, Icon icon, Icon activeIcon})
      : item = BottomNavigationBarItem(
            icon: icon,
            activeIcon: activeIcon,
            title: Text(
              title,
              style: TextStyle(
                fontSize: 14,
              ),
            ),
            backgroundColor: Colors.white);
}
